<template>
  <div class="white-box">
    <div class="table-mb">
      <el-button type="primary" @click="handleAdd">添加小区</el-button>
    </div>
    <div class="task-one">
      <div class="task-one__item">
        <span class="task-one__label">小区名称：</span>
        <el-input placeholder="请输入小区名称" v-model="searchParams.min_area_title" @keyup.enter.native="handleSearch"
                  clearable></el-input>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">房源类型：</span>
        <el-select v-model="searchParams.house_type_id" placeholder="请选择房源类型" clearable>
          <el-option :label="item.house_tag_title" :value="item.house_tag_id" v-for="(item, index) in selectOptions.house_type_id"
                     :key="index"/>
        </el-select>
      </div>

      <div class="task-one__item">
        <span class="task-one__label">省/市/区：</span>
        <elui-china-area-dht v-model="searchRegion" @change="handleSearchRegionChange" clearable></elui-china-area-dht>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">详细地址：</span>
        <el-input placeholder="请输入详细地址" v-model="searchParams.addr" @keyup.enter.native="handleSearch"
                  clearable></el-input>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">创建时间：</span>
        <el-date-picker
            v-model="searchParamsTime"
            type="datetimerange"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            date-format="YYYY/MM/DD ddd"
            time-format="A hh:mm:ss"
        />
      </div>
      <div class="task-one__btn clearfix">
        <el-button type="primary" :loading="searchBtnLoading" :icon="Search" @click="handleSearch">搜索</el-button>
      </div>
    </div>

    <el-table class="table-mb" v-loading="loading" :data="list" :header-cell-style="{ background: '#F5F7FA' }"
              element-loading-background="rgba(255, 255, 255, 0.6)">
      <el-table-column prop="min_area_title" label="小区名称"> </el-table-column>
      <el-table-column prop="yuan" label="园/苑"> </el-table-column>
      <el-table-column prop="pianqu_title" label="所属片区"> </el-table-column>
      <el-table-column prop="house_type_title" label="房源类型"> </el-table-column>
      <el-table-column prop="addr" label="楼栋">
        <template #default="{ row }">
          <div>开始：{{row.begin_dong_num}}</div>
          <div>结束：{{row.end_dong_num}}</div>
          <div>无效：{{row.no_dong_num}}</div>
        </template>
      </el-table-column>
      <el-table-column prop="lng" label="经度,纬度">
        <template #default="{ row }">
          <div>{{row.lng}},{{row.lat}}</div>
        </template>
      </el-table-column>
      <el-table-column prop="addr" label="地址" width="260">
        <template #default="{ row }">
          <div>{{row.province}}{{row.city}}{{row.district}}</div>
          <div>{{row.addr}}</div>
        </template>
      </el-table-column>
      <el-table-column prop="created_at" label="创建时间"> </el-table-column>
      <el-table-column label="操作" width="200" fixed="right">
        <template #default="{ row }">
          <el-button link type="primary" @click="handleEdit(row,{min_area_id:row.min_area_id})">编辑</el-button>
          <el-button link type="primary" @click="handleUnit(row)">单元列表</el-button>
          <el-popconfirm :persistent="false" title="确定删除吗？" confirm-button-text="确定" cancel-button-text="取消"
                         @confirm="handleDelete(row)">
            <template #reference>
              <el-button link type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination background v-model:current-page="page" v-model:page-size="pageSize" :page-sizes="pageSizeNum"
                   :total="totalNums" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>


    <el-dialog v-model="dialogShow" :title="dialogParams.type == 'add'?'添加':'编辑'" width="660px" :append-to-body="true" :destroy-on-close="true"
               :close-on-click-modal="false" @close="closeDialog">
      <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules" label-width="120px">

        <el-form-item label="小区名称" prop="min_area_title" required>
          <el-input v-model.trim="dialogForm.min_area_title" clearable placeholder="请输入小区名称"/>
        </el-form-item>
        <el-form-item label="园/苑" prop="yuan" required>
          <el-input v-model.trim="dialogForm.yuan" clearable placeholder="请输入园/苑"/>
        </el-form-item>

        <el-form-item label="房源类型" prop="house_type_id" required>
          <el-select v-model="dialogForm.house_type_id" placeholder="请选择房源类型">
            <el-option :label="item.house_tag_title" :value="item.house_tag_id" v-for="(item, index) in selectOptions.house_type_id"
                       :key="index"/>
          </el-select>
        </el-form-item>

        <el-form-item label="片区" prop="pianqu_id" required>
          <el-select v-model="dialogForm.pianqu_id" placeholder="请选择片区">
            <el-option :label="item.pianqu_title" :value="item.pianqu_id" v-for="(item, index) in selectOptions.pianqu_id" :key="index"/>
          </el-select>
        </el-form-item>
        <el-form-item label="详细地址" prop="addr" required>
          <el-input v-model.trim="dialogForm.addr" clearable placeholder="请输入详细地址"  :rows="2" type="textarea"/>
        </el-form-item>

        <el-form-item label="开始栋" prop="begin_dong_num" required>
          <el-input v-model.trim="dialogForm.begin_dong_num" clearable placeholder="请输入开始栋" style="width: 30%"/>
          <el-text type="danger" style="margin-left: 5px">格式：纯数字或者纯字母</el-text>
        </el-form-item>
        <el-form-item label="结束栋" prop="end_dong_num" required>
          <el-input v-model.trim="dialogForm.end_dong_num" clearable placeholder="请输入结束栋" style="width: 30%"/>
          <el-text type="danger" style="margin-left: 5px">格式：纯数字或者纯字母</el-text>
        </el-form-item>
        <el-form-item label="无效栋" prop="no_dong_num">
          <el-input v-model.trim="dialogForm.no_dong_num" clearable placeholder="请输入无效栋" style="width: 30%"/>
          <el-text type="danger" style="margin-left: 5px">格式：纯数字或者纯字母;多个使用 | 分隔</el-text>
        </el-form-item>
        <el-row :gutter="10">
          <el-col :span="10">
            <el-form-item label="纬度" prop="lat">
              <el-input v-model.trim="dialogForm.lat" clearable placeholder="请输入纬度" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="经度" prop="lng" label-width="50px">
              <el-input v-model.trim="dialogForm.lng" clearable placeholder="请输入经度" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-button type="primary" @click="handleLon">获取经纬度</el-button>
          </el-col>
        </el-row>

        <el-form-item>
          <div class="flexs">
            <el-button type="primary" :loading="dialogParams.loading" @click="submitDialogForm(dialogFormRef)">
              确认
            </el-button>
            <el-button @click="resetDialogForm(dialogFormRef)">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>

    <go-back/>
  </div>
</template>

<script setup>
import {
  getCurrentInstance,
  ref,
  reactive,
  onMounted,
  watch
} from "vue"
import listMixins from "@mixins/list"
import {
  useRouter
} from "vue-router"
import {
  Search
} from "@element-plus/icons-vue"
import GoBack from "@components/GoBack.vue"
import {EluiChinaAreaDht} from "elui-china-area-dht";
import {validNumOrLetter} from "@common/utils";

const router = useRouter()
const {
  proxy
} = getCurrentInstance()

/**
 * minxin列表加载
 */
const {
  page,
  pageSizeNum,
  pageSize,
  loading,
  totalNums,
  searchParams,
  searchBtnLoading,
  apiPath,
  list,
  dialogFormRef,
  dialogShow,
  dialogForm,
  dialogParams,
  handleSearch,
  getData,
  handleAdd,
  handleEdit,
  resetDialogForm,
  handleAllSelected,
  submitDialogForm,
  closeDialog,
  handleAction
} = listMixins()

dialogForm.min_area_title = '';
dialogForm.house_type_id = '';
dialogForm.yuan = '';
dialogForm.pianqu_id = '';
dialogForm.addr = '';
dialogForm.begin_dong_num = '';
dialogForm.end_dong_num = '';
dialogForm.no_dong_num = '';
dialogForm.lat = '';
dialogForm.lng = '';

apiPath.value = 'admin/minArea/index';
dialogParams.addApiPath = 'admin/minArea/create';
dialogParams.editApiPath = 'admin/minArea/update';

const searchParamsTime = ref('');
watch(searchParamsTime,(newV, oldV)=>{
  if(!newV){
    searchParams.s_time = ''
    searchParams.e_time = ''
  }else{
    searchParams.s_time = newV[0]
    searchParams.e_time = newV[1]
  }
})
const searchRegion = reactive([])
const handleSearchRegionChange =(e)=>{
  if(!e){
    searchParams.province_code = ''
    searchParams.city_code = ''
    searchParams.district_code = ''

  }else{
    searchParams.province_code = e[0]
    searchParams.city_code = e[1]
    searchParams.district_code = e[2]
  }
}

onMounted(() => {
  getData()
  getHouseTypeSelect()
  getPainQuSelect()
})

const dialogFormRules = reactive({
  min_area_title:[{
    required: true,
    message: "请输入小区名称",
    trigger: "blur"
  }],
  house_type_id: [{
    required: true,
    message: "请输入房源类型",
    trigger: "change"
  }],
  pianqu_id: [{
    required: true,
    message: "请输入片区",
    trigger: "change"
  }],
  yuan:[{
    required: true,
    message: "请输入园/苑",
    trigger: "blur"
  }],
  addr:[{
    required: true,
    message: "请输入详细地址",
    trigger: "blur"
  }],
  begin_dong_num:[{
    required: true,
    message: "请输入开始栋数",
    trigger: "blur"
  },{
    validator: proxy.$util.validNumOrLetter,
    trigger: "blur"
  }],
  end_dong_num:[{
    required: true,
    message: "请输入结束栋数",
    trigger: "blur"
  },{
    validator: proxy.$util.validNumOrLetter,
    trigger: "blur"
  }],
  lat: [{
    required: true,
    message: "请输入纬度",
    trigger: "blur"
  }],
  lng: [{
    required: true,
    message: "请输入经度",
    trigger: "blur"
  }],
})


const handleDelete=(row)=> {
  handleAction('admin/minArea/delete', {
    min_area_id: row.min_area_id,
  })
}

const handleUnit = (row) => {
  router.push({
    path: "/minAreaUnit/minAreaUnit",
    query: {
      min_area_id: row.min_area_id,
      metaTitle:`【${row.min_area_title}】单元列表`
    },
  })
}

const selectOptions = reactive({
  house_type_id:[],
  pianqu_id:[]
})
const getHouseTypeSelect = ()=>{
  proxy.$axios
      .get("common/dataSelect/houseTagSelect",{params: {p_id:1}})
      .then((res) => {
        if (res.data.code == 200) {
          selectOptions.house_type_id = res.data.data
        } else {
          proxy.$message({
            message: res.data.msg,
            type: "error"
          })
        }
      })
      .catch((err) => {
      })
}
const getPainQuSelect = () => {
  proxy.$axios
      .get("common/dataSelect/painQuSelect")
      .then((res) => {
        if (res.data.code == 200) {
          selectOptions.pianqu_id = res.data.data
        } else {
          proxy.$message({
            message: res.data.msg,
            type: "error"
          })
        }
      })
      .catch((err) => {
      })
}
const handleLon = ()=> {
  window.open("https://lbs.qq.com/getPoint/");
}

</script>

<style lang="less">

</style>